/**
  ******************************************************************************
  * @file    shoot task.h
  * @author  Wang Hongxi
  * @version V1.0.0
  * @date    2019/12/31
  * @brief   
  ******************************************************************************
  * @attention 
  *
  ******************************************************************************
  */
#ifndef _SHOOT_TASK_H
#define _SHOOT_TASK_H

#include "includes.h"

#define SHOOT_TASK_PERIOD 2

// Fric motor configuration
#define FRIC_MOTOR_USE_PWM

#ifdef FRIC_MOTOR_USE_RM3508
#include "bsp_CAN.h"
#define FRIC_RM3508_LEFT_ID 0x207
#define FRIC_RM3508_RIGHT_ID 0x208
#define FRIC_MOTOR_MIN_RPM 2500
#define FRIC_MOTOR_MAX_RPM 9500
#else
#define FRIC_MOTOR_USE_PWM
#include "bsp_PWM.h"
#define FRIC_MOTOR_TIM htim4
#define FRIC_MOTOR_TIM_CHANNEL_1 TIM_CHANNEL_1
#define FRIC_MOTOR_TIM_CHANNEL_2 TIM_CHANNEL_2
#define FRIC_MOTOR_SUSPEND_CCR 1000
#define FRIC_MOTOR_MIN_CCR 1200
#define FRIC_MOTOR_MAX_CCR 2000
#endif

// Trigger motor configuration
#define TRIGGER_MOTOR_ID 0x206
#define TRIGGER_MOTOR_REDUCTION_RATIO 36
#define BULLETS_PER_ROUND 4

enum
{
    NoShooting = 0,
    DebugShoot,
    OneShot,
    CounterShot,
    SHOOTNONEEE,
    SHOOTNONEEEE,
    SHOOTNONEEEEE,
    SHOOTNONEEEEEE,
    SHOOTNONEEEEEEE
};

typedef struct _shoot
{
    uint8_t ShootMode;

    int32_t BulletToShoot;

    float TriggerSpeed;
    Motor_t TriggerMotor;

    float SpeedInBulletsPerSec;
    uint32_t NumsOfOneShot;
    uint32_t ShootDelayInMs;

#ifdef FRIC_MOTOR_USE_RM3508
    float FricSpeed;
    float RealFricSpeed;
    float RealFricSpeed_dot;
    Ordinary_Least_Squares_t FricSpeed_OLS;
    Motor_t FricMotor[2];
#else
    TIM_HandleTypeDef PWM_TIM;
    uint32_t FricPWM;
    uint8_t PWM_CHANNEL_1;
    uint8_t PWM_CHANNEL_2;
#endif
} Shoot_t;

extern Shoot_t Shoot;

void Shoot_Init(void);
void Shoot_Control(void);
float ShootAndDelay(Motor_t *trigger, float speedInNumsPerSec, uint32_t NumsOfOneShot, uint32_t delayTimeInMs);
float RotateAngleInDegree(Motor_t *trigger, float speedRPM, float angleInDegree);

#endif
